package com.yc.example.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.yc.example.bean.Msg;
import com.yc.example.entity.User;
import com.yc.example.util.DbUtil;

public class UserDao {

private Connection con;
	
	private PreparedStatement stmt;
	
	//用户登录
	public Msg login(User user){
		
		try {
			con = DbUtil.getCon(); 
			String sql = "SELECT * FROM user WHERE user_name = ?";
			stmt = con.prepareStatement(sql);
			stmt.setString(1, user.getUserName());
			ResultSet rs = stmt.executeQuery();
			if(rs.next()){
				if(user.getPassword().equals(rs.getString("password"))){
					user.setUserId(rs.getInt("user_id"));
					user.setUserName(rs.getString("user_name"));
					return new Msg("登录成功",user);
				}
				return new Msg("密码错误",null);
			}	
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try{
				DbUtil.close(stmt, con);
			} catch(SQLException e){
				e.printStackTrace();
			}
		}
		return new Msg("该用户不存在",null);
	}
	
}
